package com.caifu.filter;

import com.caifu.pojo.SysUser;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.web.filter.authc.FormAuthenticationFilter;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

/**
 * @author tww
 * ClassName: LoginAuthenticationFilter.java、
 * createTime: 2020年12月28日 16:59:38
 **/
public class LoginAuthenticationFilter extends FormAuthenticationFilter {



    @Override
    protected boolean isAccessAllowed(ServletRequest request, ServletResponse response, Object mappedValue) {


        if (isLoginRequest(request, response)) {
            if (isLoginSubmission(request, response)) {
                //本次用户登陆账号
                String account = this.getUsername(request);

                Subject subject = this.getSubject(request, response);
                //之前登陆的用户
                SysUser user = (SysUser) subject.getPrincipal();
                //如果两次登陆的用户不一样，则先退出之前登陆的用户
                if (account != null && user != null && !account.equals(user.getUserId())) {
                    subject.logout();
                }
            }
        }

        return super.isAccessAllowed(request, response, mappedValue);

    }
}
